package cm.just.hero;

import cm.just.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "ListServlet",value = "/hero/list")
public class ListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.print("<table border='1'>");
        pw.print("<caption>英雄列表</caption>");
        pw.print("<tr><th>英雄编号</th><th>英雄名称</th><th>英雄种类</th><th>英雄价格</th><th>操作</th>");
        Connection conn = null;
        try {
            conn = JDBCUtil.getConnect();
            String sql = "select id,name,type,money from hero";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String type = rs.getString("type");
                int money = rs.getInt("money");
                pw.print("<tr>");
                pw.print("<td>"+id+"</td>");
                pw.print("<td>"+name+"</td>");
                pw.print("<td>"+type+"</td>");
                pw.print("<td>"+money+"</td>");
                pw.print("<td><a href='#'>删除</a> <a href='#'>修改</a></td>");
                pw.print("</tr>");
            }
            pw.print("</table>");
            pw.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
